import 'package:flutter/material.dart';
import 'package:flutter2/colors.dart';
import 'package:flutter2/http/api.dart';
import 'package:flutter2/http/dio_manager.dart';
import 'package:flutter2/model/bank_card_entity.dart';
import 'package:flutter2/ui/add_card.dart';
import 'package:flutter2/utils/sp.dart';
import 'package:flutter2/utils/string.dart';
import 'package:flutter2/utils/user.dart';
import 'package:flutter2/widget/base/base_container.dart';
import 'package:flutter2/widget/base/normal_button.dart';
import 'package:flutter2/widget/base/normal_text.dart';
import 'package:flutter2/widget/info_view.dart';
import 'package:flutter2/widget/title_bar.dart';
import 'package:get/get.dart';

class CardPage extends StatefulWidget {
  const CardPage({super.key});

  @override
  State<CardPage> createState() => _CardPageState();
}

class _CardPageState extends State<CardPage> {
  final cards = <BankCardEntity>[];
  BankCardEntity? select;

  @override
  void initState() {
    super.initState();
    DioManager.request<List<BankCardEntity>>(
      path: HttpApi.bankCardList,
      onSuccess: (data) {
        cards.addAll(data ?? []);
        String id = SPUtil.getString("${UserUtil.getUser().userId}card", "");
        select = cards.firstWhereOrNull((element) => element.id == id);
        setState(() {});
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: TitleBar.buildAppBar(title: "银行卡"),
      body: SafeArea(
        child: Column(
          children: [
            Expanded(
              child: SingleChildScrollView(
                physics: BouncingScrollPhysics(),
                child: Column(
                  children: cards.map((e) {
                    return getItem(e, cards.indexOf(e));
                  }).toList(),
                ),
              ),
            ),
            NormalButton(
              width: double.infinity,
              margin: EdgeInsets.all(16),
              text: "添加银行卡",
              click: () {
                Get.to(AddCardPage(loan: false));
              },
            )
          ],
        ),
      ),
    );
  }

  Widget getItem(BankCardEntity card, int index) {
    return BaseContainer(
      background: Colors.white,
      radius: 10,
      padding: EdgeInsets.symmetric(horizontal: 16, vertical: 15),
      margin: EdgeInsets.only(left: 16, right: 16, top: index == 0 ? 10 : 5, bottom: 5),
      child: Column(
        children: [
          Row(
            children: [
              BaseContainer(
                backgroundImg: "ic_coin".toImage(),
                width: 25,
                height: 25,
              ),
              SizedBox(
                width: 10,
              ),
              NormalText(
                fontSize: 14,
                fontColor: mainText,
                text: card.bankTypeDesc,
                fontWeight: FontWeight.bold,
              ),
            ],
          ),
          SizedBox(
            height: 5,
          ),
          InfoView(
            leftText: "Banco emisor",
            rightText: card.bankName,
          ),
          InfoView(
            leftText: "Número de tarjeta",
            rightText: card.no,
          ),
          SizedBox(height: 5),
          Row(
            children: [
              BaseContainer(
                click: () {
                  select = card;
                  setState(() {});
                  SPUtil.putString("${UserUtil.getUser().userId}card", card.id);
                },
                padding: EdgeInsets.only(top: 5, bottom: 5, right: 5),
                child: BaseContainer(
                  backgroundImg: card == select ? "cb_checked".toImage() : "cb_unchecked".toImage(),
                  width: 16,
                  height: 16,
                ),
              ),
              Expanded(
                child: NormalText(
                  fontSize: 12,
                  fontColor: mainText2,
                  text: "Tarjeta receptora por defecto",
                ),
              )
            ],
          )
        ],
      ),
    );
  }
}
